<template>
	<!-- 跟进日报 -->
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="80px">
			<el-row class="search-row">
				<el-col :span="12">
					<el-form-item prop="name" label="客户名字">
						<el-input v-model="dataForm.name" placeholder="客户名字"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item prop="mobile" label="电话号码">
						<el-input v-model="dataForm.mobile" placeholder="电话号码"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row class="search-row">
				<el-col :span="12">
					<el-form-item prop="market" label="市场">
						<el-input v-model="dataForm.market" placeholder="市场"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item prop="business" label="生意">
						<el-input v-model="dataForm.business" placeholder="生意"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row class="search-row">
				<el-col :span="12">
					<el-form-item prop="shopNo" label="店铺号码">
						<el-input v-model="dataForm.shopNo" placeholder="店铺号码"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<el-table
			:data="dataForm.followUpDetailsVOList"
			border
			style="width: 100%"
		>
			<!-- <el-table-column type="selection" label="序号" header-align="center" align="center" width="50"></el-table-column> -->
			<el-table-column prop="customerName" label="跟进客户" header-align="center" align="center">
			</el-table-column>
			<el-table-column prop="method" label="跟进方式" header-align="center" align="center">
			</el-table-column>
			<el-table-column prop="progress" label="跟进情况" header-align="center" align="center">
			</el-table-column>
			<el-table-column prop="remark" label="备注" header-align="center" align="center">
			</el-table-column>
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="170">
				<template #default="scope">
					<el-button type="primary" link @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
					<el-button type="primary" link @click="deleteBatchHandle(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import {  useClientApi, useClientSubmitApi } from '@/api/client'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()
const dataForm = reactive({
	id: '',
	dictTypeId: 0,
	dictLabel: '',
	dictValue: '',
	sort: 0,
	remark: '',
	followUpDetailsVOList: []

})
// const followList = reactive([])

const dataRules = ref({
	dictLabel: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	dictValue: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	// id 存在则为修改
	if (id) {
		getClientData(id)
	}
}

const getClientData = (id: number) => {
	useClientApi(id).then(res => {
		Object.assign(dataForm, res.data)
		// Object.assign(followList, res.data.followUpDetailsVOList)
	})
}

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}
		useClientSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init,
	dataForm
})
</script>
